package com.ds.app.util;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ds.app.App;
import com.ds.app.AppCacheManager;
import com.ds.app.AppNotFoundException;
import com.ds.app.Module;
import com.ds.context.JDSActionContext;
import com.ds.server.eumus.ConfigCode;


public class ModuleExtMenu
{   

    private List modules;
	private App[] apps;
	private App topApp;
	private String topAppId;

	public ModuleExtMenu(List modules,String topAppId) {   
		  Map appMap = new HashMap();	
		  this.topAppId=topAppId;
	  		for (int i = 0; i < modules.size(); i++) {
	  			List<App> appList = ((Module)modules.get(i)).getAppList();
	  			if (appList != null) {
	  				for (int j = 0; j < appList.size(); j++) {
	  					if (!appMap.containsKey(appList.get(j).getID())){
	  						addApp(appMap, appList.get(j));
	  					}
	  				}
	  			}
	  		}		
	  		this.apps = (App[])appMap.values().toArray(new App[0]);

    	this.modules=modules;
	}
    
	
	  public String getChildTree(String appId) {	
		  if (appId==null ||appId.equals(ModuleExtMenuTree.TOPID)){
			  return this.getTopTree();
		  }
		  
		  App app=null;
		try {
			app = AppCacheManager.getInstance(ConfigCode.fromType(JDSActionContext.getActionContext().getSystemCode())).getAppByID(appId);
		} catch (AppNotFoundException e) {
			e.printStackTrace();
		}
		ModuleExtMenuNode childNode = new ModuleExtMenuNode();
		if (app==null ||!isAppInApps(app,apps)){

			 childNode.setId(app.getID());
	    		childNode.setText("读取错误");
	    		childNode.setCnName("读取错误");
	    		childNode.setUid("orgerr");
	    		childNode.setLeaf("true");	
		}else{
		    	childNode.setId(app.getID());
	    		childNode.setText(app.getName());
	    		childNode.setCnName(app.getName());
	    		childNode.setUid(app.getID());
	    		
	    		addModuleItem(childNode,app);
	    		if (childNode.getChildren().size()>0){
	    			childNode.setLeaf("true");	
	    		}else{
	    			childNode.setLeaf("false");	
	    		}
	    		if (app.getChildrenList().size()>0){
	   			 List<App> ccapps=app.getChildrenList();
	   				for (int j=0;j<ccapps.size();j++){   					
	   					App ccorg=ccapps.get(j);
	   					if (isAppInApps(ccorg,apps)){
	   						addChildNode(childNode,ccorg,apps);
	   					}    					
	   				}
	   			}
		  
		}
			
		    ModuleExtMenuTree tree = new ModuleExtMenuTree();
	        tree.setNode(childNode);
	        StringBuffer layoutJson = new StringBuffer();
	        tree.serializChild(layoutJson);
	        return layoutJson.toString();
		}
 
	
	
    private  String getTopTree()
    {
        String rootStr="点击选择应用树";
        ModuleExtMenuNode root = new ModuleExtMenuNode(); 
        root.setId("root");
        root.setLeaf("false");
        root.setText(rootStr);
        root.setCnName(rootStr);
        root.setUid("root");
         if (apps.length==0){
        	rootStr="没有应用模块";
        	root.setText(rootStr);
            root.setCnName(rootStr);
            root.setLeaf("true");
        }else{
        	addChildNode(root,this.topApp,apps);
        }
        ModuleExtMenuTree tree = new ModuleExtMenuTree();
        tree.setNode(root);
        StringBuffer layoutJson = new StringBuffer();
        tree.serializ(layoutJson);
        return layoutJson.toString();
    }
    
    /**
	 * @param map
	 * @param App
	 */
	private void addApp(Map map, App app) {
		if (app != null) {
			map.put(app.getID(), app);
			App parent = app.getParent();
			
			if (parent != null){
				if (this.topAppId!=null && parent.getID().equals(topAppId)){
					this.topApp=parent;
					map.put(parent.getID(), parent);
				}else{
					if (!map.containsKey(parent.getID()) ){
						addApp(map, parent);
				     }
				}
				
			}else{
				this.topApp=app;	
			}
			
		}
			
	}
	
  

   
	private boolean isAppInApps(App app,App[] apps){	
		for(int i=0; i<apps.length; i++) {			
		    if(app.getID().equals(apps[i].getID())) {
		        return Boolean.TRUE;
		    }
		}
		return false;
	}

	protected void addModuleItem(ModuleExtMenuNode parentnode,App app) {
				
		for(int i=0;this.modules.size()>i;i++){
			Module module=(Module) modules.get(i);
			if (this.isModuleInApp(app, module.getID())){
				ModuleExtMenuNode inputNode = new ModuleExtMenuNode();
					inputNode.setId(module.getID());	
					inputNode.setText(module.getName());
					inputNode.setCnName(module.getName());
					inputNode.setUid(module.getID());
				
					inputNode.setLeaf("true");
					parentnode.addChild(inputNode);
					inputNode.setParent(parentnode);
			};
		}
		
		
	}
	private boolean isModuleInApp(App app,String moduleId){
		List<Module> modules = app.getModuleList();
		for(Module module:modules) {
		    if(moduleId.equalsIgnoreCase(module.getID())) {
		        return Boolean.TRUE;
		    }
		}
		return false;
	}

 
    
    private ModuleExtMenuNode addChildNode(ModuleExtMenuNode parentnode,App app , App[] apps) {	
    	if(isAppInApps(app,apps)){    
    		ModuleExtMenuNode inputNode = new ModuleExtMenuNode();
			inputNode.setId(app.getID());
			inputNode.setText(app.getName());
			inputNode.setCnName(app.getName());
			inputNode.setUid(app.getID());
			parentnode.addChild(inputNode);
			inputNode.setParent(parentnode);
			inputNode.setLeaf("false");		
			addModuleItem(inputNode,app);
			
//			if (org.getChildren().length>0){
//			 Org[] ccorgs=org.getChildren();
//				for (int j=0;j<ccorgs.length;j++){   					
//					Org ccorg=ccorgs[j];
//					if (isOrgInOrgs(ccorg,orgs)){
//						addChildNode(inputNode,ccorg,orgs);
//					}    					
//				}
//			}
			
 		}
    	
		return parentnode;

	}
   
   

}
